package com.asiainfo.springcloud.manager;

import de.codecentric.boot.admin.event.ClientApplicationEvent;
import de.codecentric.boot.admin.event.ClientApplicationStatusChangedEvent;
import de.codecentric.boot.admin.notify.AbstractStatusChangeNotifier;
import de.codecentric.boot.admin.notify.LoggingNotifier;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.expression.EvaluationContext;
import org.springframework.expression.Expression;
import org.springframework.expression.ParserContext;
import org.springframework.expression.spel.standard.SpelExpressionParser;
import org.springframework.expression.spel.support.StandardEvaluationContext;


public class SMSNotifier extends AbstractStatusChangeNotifier {
    private static final Logger logger = LoggerFactory.getLogger(SMSNotifier.class);

    private static final String DEFAULT_TEXT = "#{application.name} (#{application.id})\nstatus changed from #{from.status} to #{to.status}\n\n#{application.healthUrl}";
    //private static final String DEFAULT_DESCRIPTION = "<strong>#{application.name}</strong>/#{application.id} is <strong>#{to.status}</strong>";
    private Expression text;
    private final SpelExpressionParser parser = new SpelExpressionParser();
    //private final SMSSender smsSender;

    public SMSNotifier() {
        System.out.println("这个类被初始化了.......");
        //this.smsSender = smsSender;
        this.text = parser.parseExpression(DEFAULT_TEXT, ParserContext.TEMPLATE_EXPRESSION);
    }

    @Override
    protected void doNotify(ClientApplicationEvent event) throws Exception {
        EvaluationContext context = new StandardEvaluationContext(event);
        logger.info("Application {} ({}) is {}", event.getApplication().getName(),
                event.getApplication().getId(), ((ClientApplicationStatusChangedEvent) event).getTo().getStatus());
        System.out.println(String.format("Application {} ({}) is {}", event.getApplication().getName(),
                event.getApplication().getId(), ((ClientApplicationStatusChangedEvent) event).getTo().getStatus()));
        logger.info("Application {} ({}) is {}", event.getApplication().getName(),
                event.getApplication().getId(), ((ClientApplicationStatusChangedEvent) event).getTo().getStatus());
        System.out.println("这里的内容是要发短信的："+text.getValue(context, String.class).toString());
        System.out.println("将这里的逻辑替换成发短信使用的");
    }

}
